package demo3;

import java.io.File;

import java.io.FileFilter;

import java.io.IOException;

import java.util.Scanner;


public class Test10 {

    public static void main(String[] args) {

        //1.键盘输入目录名

        Scanner scanner = new Scanner(System.in);

        String name = scanner.nextLine();

        //2.创建文件和目录--参考程序1

        File dir=new File(name);

        //File dir=new File(name);


        if (!dir.exists()){

            dir.mkdir();

        }

        File file1=new File(dir,"1.txt");

        File file2=new File(dir,"2.txt");

        File file3=new File(dir,"3.java");

        try {

            file1.createNewFile(); //文件所在目录不存在，会引发异常

            file2.createNewFile();

            file3.createNewFile();

        } catch (IOException e) {

            System.out.println("文件创建失败！");;

        }

        //3.遍历目录、过滤输出---参考程序2

        File[] files = dir.listFiles(new FileFilter() {

            @Override

            public boolean accept(File pathname) {

                return pathname.getName().endsWith(".txt");

            }

        });

        if (files==null){

            System.out.println("无结果");

        }else {

            for (File file : files) {

                System.out.println(file.getName());

            }

        }

    }

}



